package com.sunda.spmsweb.ibmmq;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.JmsTemplate;

/**
 * @program: spms
 * @description: Jms config - message in. 监听传进本系统的消息队列。
 * @author: Wayne Wu
 * @create: 2020-11-03 13:59
 **/
@Configuration
public class JmsConfigIn {

    /** 开发测试环境队列配置 */
    //public static final String queue = "SUNDA.SPMS.DATA.TEST01";

    /** 生产环境队列配置 */
    //public static final String queue = "SUNDA.SPMS.DATA.CN01";

    public static String queue;
    @Value("${project.mq.out.queue}")
    public void setQueue(String queue){
        JmsConfigIn.queue = queue;
    }

    @Value("${project.mq.out.host}")
    private String host;
    @Value("${project.mq.out.port}")
    private Integer port;
    @Value("${project.mq.out.queue-manager}")
    private String queueManager;
    @Value("${project.mq.out.channel}")
    private String channel;
    @Value("${project.mq.out.username}")
    private String username;
    @Value("${project.mq.out.password}")
    private String password;
    @Value("${project.mq.out.receive-timeout}")
    private int receiveTimeout;
    @Value("${project.mq.out.ccsid}")
    private int CCSID;

    @Bean(name = "PIN_IN")
    public MQQueueConnectionFactory mqQueueConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        mqQueueConnectionFactory.setHostName(host);
        try {
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setCCSID(CCSID);
            mqQueueConnectionFactory.setChannel(channel);
            mqQueueConnectionFactory.setPort(port);
            mqQueueConnectionFactory.setQueueManager(queueManager);
            mqQueueConnectionFactory.setClientReconnectTimeout(receiveTimeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqQueueConnectionFactory;
    }

    @Bean(name = "PIN_IN1")
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(@Qualifier(value = "PIN_IN") MQQueueConnectionFactory mqQueueConnectionFactory) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername(username);
        userCredentialsConnectionFactoryAdapter.setPassword(password);
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
        return userCredentialsConnectionFactoryAdapter;
    }

    @Bean(name = "PIN_IN2")
    @Primary
    public CachingConnectionFactory cachingConnectionFactory(@Qualifier(value = "PIN_IN1") UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
        cachingConnectionFactory.setSessionCacheSize(500);
        cachingConnectionFactory.setReconnectOnException(true);
        return cachingConnectionFactory;
    }

    @Bean(name = "PIN_IN3")
    public JmsOperations jmsOperations(@Qualifier(value = "PIN_IN2") CachingConnectionFactory cachingConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
        jmsTemplate.setReceiveTimeout(receiveTimeout);
        return jmsTemplate;
    }

    @Bean(name = "jmsListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(@Qualifier(value = "PIN_IN2") CachingConnectionFactory cachingConnectionFactory) {
        DefaultJmsListenerContainerFactory factory =
                new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingConnectionFactory);
        //设置连接数
        factory.setConcurrency("3-10");
        //重连间隔时间
        factory.setRecoveryInterval(1000L);
        return factory;
    }


}
